package com.moorror.codegenvel.view.window.codegenxrender.view;


import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBTabbedPane;
import com.moorror.codegenvel.tab.TabJsonWrap;
import com.moorror.codegenvel.common.utils.JacksonUtils;
import com.moorror.codegenvel.db.sqlite.entity.CodeGenXRenderTableConfig;
import com.moorror.codegenvel.view.window.codegen.view.TabWrapTabCodeGenSceneKeyFields;
import com.moorror.codegenvel.view.window.codegenxrender.domain.JsonTableConfigPO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;

/**
 * @Author jiyanghuang
 * @Date 2025/8/15 22:40
 */
public class NameTableConfigPanel extends JPanel {

    private Project project;

    private JTabbedPane tabbedPane;

    private TabJsonWrap templateJsonValue;

    private CodeGenXRenderTableConfig model;

    private TabWrapTabCodeGenSceneKeyFields keyFields;

    private TabWrapTabNameTableConfigPanel tab;

    public NameTableConfigPanel(Project project, CodeGenXRenderTableConfig model) {
        this.project = project;
        this.model = model;
        initView();
    }

    private void initView(){
        setLayout(new BorderLayout(0,0));
        tabbedPane = new JTabbedPane(JBTabbedPane.TOP);
        tab = new TabWrapTabNameTableConfigPanel(project, 0, "Schema", tabbedPane, new TabGenCodeSchema(this.project, this.model));
        add(tabbedPane, BorderLayout.CENTER);
        fillValue();
    }

    public void fetchValue(){
        JsonTableConfigPO jsonTableConfigPO = new JsonTableConfigPO();
        TabGenCodeSchema tabGenCodeSchema = tab.getTabGenCodeSchema();
        jsonTableConfigPO.setTypeSchema(tabGenCodeSchema.getTypeSchema());
        jsonTableConfigPO.setColumnDescList(tabGenCodeSchema.columnList());
        model.setJsonTableConfig(JacksonUtils.toJSONString(jsonTableConfigPO));
    }

    public void fillValue(){
        if(StringUtils.isNotBlank(model.getJsonTableConfig())){
            JsonTableConfigPO jsonTableConfigPO = JacksonUtils.toJavaObject(model.getJsonTableConfig(), JsonTableConfigPO.class);
            TabGenCodeSchema tabGenCodeSchema = tab.getTabGenCodeSchema();
            tabGenCodeSchema.setTypeSchema(jsonTableConfigPO.getTypeSchema());
            if(CollectionUtils.isNotEmpty(jsonTableConfigPO.getColumnDescList())){
                tabGenCodeSchema.fillColumnList(jsonTableConfigPO.getColumnDescList());
            }
        }
    }

    public void fillSchemaJson(String json){
        tab.fillSchemaJson(json);
    }

    public String fetchSchemaJson(){
        return tab.fetchSchemaJson();
    }
}
